package com.petiot.rabbitmq;

import java.util.List;
import java.util.UUID;

import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

public class CommandPush {
	private String TAG = CommandPush.class.getSimpleName();
	
	String group_id = "A0000";
	String myclient_uid = "ZHUWEI_WAVIOT_VDEVICE_20151111_1";
	private String wavcam_device_uid = "ZHUWEI_WAVIOT_VDEVICE_20151111_1";
	private String replyQueueName;
	private static final String EXCHANGE_NAME = "petiot.direct";
	private QueueingConsumer consumer;
	public WavIoTClient waviotClient = new WavIoTClient(group_id, myclient_uid);
	private String msg;
	
	Thread publishThread;

	public void motor_run(String MSG) {
		// TODO Auto-generated method stub
		msg = MSG;
		publishThread = new Thread(new Runnable() {

			@Override
			public void run() {
				try {

					replyQueueName = waviotClient.getChannel().queueDeclare().getQueue();
					waviotClient.getChannel().queueBind(replyQueueName, EXCHANGE_NAME, replyQueueName);

					consumer = new QueueingConsumer(waviotClient.getChannel());
					String consume_tag = waviotClient.getChannel().basicConsume(replyQueueName, true, consumer);
					waviotClient.getChannel().confirmSelect();
					System.out.println("AMQP RPCClient is success replyQueueName = " + replyQueueName);
					System.out.println("AMQP RPCClient is success consume_tag = " + consume_tag);
					System.out.println("bbbbbbbb");
					while (true) {
						System.out.println("22222222222222bbbbbbbb");

						String corrId = UUID.randomUUID().toString();
						// channel.queueBind(requestQueueName, EXCHANGE_NAME,
						// wavcam_device_uid);

						try {
							BasicProperties props = new BasicProperties.Builder().correlationId(corrId)
									.replyTo(replyQueueName).build();
							System.out.println("1010101010" + props);

							waviotClient.getChannel().basicPublish(EXCHANGE_NAME, wavcam_device_uid, props,
									msg.getBytes("UTF-8"));
							Log.d("", "[s] " + msg);

							waviotClient.getChannel().waitForConfirmsOrDie();
							// channel.queueUnbind(requestQueueName,
							// EXCHANGE_NAME, wavcam_device_uid);

							CommandPush fibonacciRpc = null;
							String response = null;
							try {
								// fibonacciRpc = new RPCClientActivity();

								System.out.println(" [x] Requesting fib(30)");
								while (true) {
									QueueingConsumer.Delivery delivery = consumer.nextDelivery();
									if (delivery.getProperties().getCorrelationId().equals(corrId)) {
										response = new String(delivery.getBody(), "UTF-8");
										break;
									}
								}
								// channel.queueUnbind(replyQueueName,
								// EXCHANGE_NAME, replyQueueName);
								System.out.println(" [.] Gotzzzzzzzzzzzzzzzzzzzzzt '" + response + "'");

							} catch (Exception e) {
								Log.d("7", "publishToAMQP Connection broken: " + e.getClass().getName());
							} finally {
								if (fibonacciRpc == null) {
									try {
										fibonacciRpc.close();
										waviotClient.getChannel().queueUnbind(replyQueueName, EXCHANGE_NAME,
												replyQueueName);
										// connection.close();
									} catch (Exception ignore) {
									}
								}
							}

						} catch (Exception e) {
							Log.d("", "[f] " + msg);
							throw e;
						}
					}
				} catch (InterruptedException e) {
					// break;
					Log.d("4", "publishToAMQP Connection broken: " + e.getClass().getName());
				} catch (Exception e) {
					Log.d("5", "publishToAMQP Connection broken: " + e.getClass().getName());
					try {
						Thread.sleep(5000); // sleep and then try again
					} catch (InterruptedException e1) {
						// break;
						Log.d("6", "publishToAMQP Connection broken: " + e1.getClass().getName());
					}
				}
			}
		});
		publishThread.start();
	}
	
	public void close() throws Exception {
		waviotClient.getChannel().close();
	}
}
